package main

import (
	"bufio"
	"fmt"
	"myfunction"
	"os"
)

func main() {

	//defer使用：open/close lock/unlock printHeader/printFooter
	writeFile()

}

func writeFile() {
	file, err := os.OpenFile("a.txt", os.O_EXCL|os.O_CREATE, 0666)
	if err != nil {
		//OpenFile返回的error类型是os.PathError 可以尝试转成该类型进行处理
		if pathErr, ok := err.(*os.PathError); ok {
			fmt.Println(pathErr.Err, pathErr.Op, pathErr.Path, pathErr.Timeout())
		} else {
			fmt.Print(err)
		}

	}
	defer file.Close()
	writer := bufio.NewWriter(file)
	defer writer.Flush()
	f := myfunction.Fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Fprintf(writer, "%d ", f())
	}
}
